import { createStore } from 'vuex'
import api from '@/api/index'
export default createStore({
  state: {
    playlist: [{
      al: {
        id: 1486513704,
        name: "彩劵",
        pic: 109951165386012140,
        picUrl: "http://p3.music.126.net/NP1Zg57UUbHNzAhw7n-LeA==/109951165386012146.jpg",
        pic_str: "109951165386012146"
      },
      name: "彩劵",
      id: 1486513704
    }],
    playCurrentIndex: 0,
    lyric: '',
    currentTime: 0,
    intervalId: 0,
    user: {
      isLogin: false,
      account: {},
      userDetail:{}
    }
  },
  getters: {
    lyricList:function(state){
      let arr = state.lyric.split('\n').map((item,i,arr)=>{
      let min = item.slice(1,3)
      let sec = item.slice(4,6)
      let mill = item.slice(7,10)
        return {
          min, sec, mill,
          lyric: item.slice(12, item.length),
          content:item,
          time:mill+sec*1000+min*60*1000
        }
      })
      arr.forEach((item, i) => {
        if (i == 0) {
          item.pre = 0
        } else {
          item.pre = arr[i-1].time
        }
      })
      return arr
     }
  },
  mutations: {
    setPlayList: function (state,value) {
      state.playlist = value
    },
    pushPlayList: function (state,value) {
      state.playlist.push(value)
    },
    setPlayIndex(state,value) {
      state.playCurrentIndex = value
    },
    setLyric(state,value) {
      state.lyric = value
    },
    setCurrentTime(state,value) {
      state.currentTime = value
    },
    // 修改状态
    setUser(state, value) {
      state.user = value
    }
  },
  actions: {
    async reqLyric(content, payload) {
      let e = await api.getLyric(payload.id)
      // console.log(e)
      content.commit('setLyric',e.data.lrc.lyric)
      // console.log(e.data.lrc.lyric)
    },
    // async reqLyric(content) {
    //   let e = await api.getLyric()
    //   console.log(e)
    //   content.commit('setLyric',e.data.lrc.lyric)
    // }

    // 设置登录
    async phoneLogin(content, payload) {
      // console.log(payload)
      let result = await api.phoneLogin(payload.phone, payload.password)
      if (result.data.code == 200) {
        content.state.user.isLogin = true
        content.state.user.account = result.data.account

        let userDetail = await api.userDetail(result.data.account.id)
        content.state.user.userDetail = userDetail.data
        // 设置保存
        localStorage.userData = JSON.stringify(content.state.user)
        console.log(userDetail)
        content.commit('setUser',content.state.user)
      }
      console.log(result)
      return result
    }
  },
  modules: {
  }
})
